package com.sanniu.controller;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import com.sanniu.common.Consts;
import com.sanniu.common.ControllerBase;
import com.sanniu.common.Options;
import com.sanniu.common.sms.SmsKit;
import com.sanniu.model.BaseRet;
import com.sanniu.model.User;
import com.sanniu.service.UserService;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;

import java.util.Date;


@RequestMapping(value = "${user.mapping}", viewPath = "/WEB-INF/views/user/")
public class UserController extends ControllerBase {
    @Inject
    private UserService userService;

    public void index() {
        int page = getParaToInt("page", 1);
        Page<User> userPage = userService.paginate(page, 5);
        setAttr("pageData", userPage);
        render("index.html");
    }

    public void add() {
        int id = getParaToInt("id", 0);
        if (id > 0) { //有id ，说明有数据提交过来，用来做修改的标识。
            setAttr("id", id);
        }
        render("add.html");
    }

    public void doSave() {
        String name = getPara("name");
        //int age = Integer.parseInt(getPara("age"));
        int age = getParaToInt("age");
        int id = getParaToInt("id", 0);

        User user = new User();
        user.setName(name);
        user.setAge(age);

        if (id > 0) { //说明是更新
            user.setId(id);
            user.update();
        } else { //说明是新增
            user.setCreateTime(new Date());
            user.save();
        }

        redirect("/user");
    }

    public void delUser() {
        long id = getParaToInt("id", 0);
        userService.deleteById(id);
        redirect("/user");
    }

    public void login() {
        render("login.html");
    }

    /**
     * 手机验证码登陆
     *
     * @param mobile
     * @param vcode
     */
    @EmptyValidate(value = {
            @Form(name = "mobile", message = "请输入手机号"),
            @Form(name = "vcode", message = "请输入验证码")
    }, renderType = "json")
    public void doLogin(String mobile, String vcode) {
        if (StrUtil.isMobileNumber(mobile) == false) {
            renderJson(BaseRet.message("你输入的手机号码不正确"));
            return;
        }
        if (!SmsKit.validateCode(mobile, vcode)) {
            renderJson(BaseRet.message("验证码错误"));
            return;
        }
        User user = userService.findByMobile(mobile);
        if (user == null) {//新增注册
            user = new User();
            user.setMobile(mobile);
            user.setCreateTime(new Date());
            user.setState(1);
            user.setName(mobile);
            userService.save(user);
        }
        setJwtAttr(Consts.JWT_USERID, user.getId());
        renderJson(BaseRet.data(user));
    }


    public void info() {
        render("info.html");
    }


    @EmptyValidate(value = {
            @Form(name = "mobile", message = "请输入手机号")
    }, renderType = "json")
    public void getSMSVCode(String mobile) {
        String code = SmsKit.generateCode();
        String template = Options.get(Consts.OPTION_REG_SMS_VALIDATE_TEMPLATE);
        String sign = Options.get(Consts.OPTION_REG_SMS_VALIDATE_SIGN);
        BaseRet br = SmsKit.sendCode(mobile, code, template, sign);
        renderJson(br);
    }
}
